package com.is.add.event;


import com.is.add.util.ServletUtils;
import com.is.add.util.SpringContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: zhou
 * @Description:  日志参数封装类
 * @Date:Create in 2019/07/01
 * @Modified By:
 */
public class ApiLogPublisher {

    public ApiLogPublisher() {
    }
    /**
     * 日志参数封装
     * @author zhou
     * @date 2019/7/1
     * @param  * @param logAction
     * @param requestId 请求Ip
     * @return void
     */
    public static void publishEvent(String logAction,Integer requestId) {
        HttpServletRequest request =  ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        Object userId = request.getSession().getAttribute("userId");
        if(userId == null){
            return;
        }
        requestId = Integer.parseInt(userId.toString()
        ) ;
        LogApi logApi = new LogApi();
        logApi.setType("1");  //操作日志
        logApi.setTitle(logAction);
        logApi.setTime(String.valueOf(System.currentTimeMillis()));
        logApi.setCreateBy(requestId.toString());
        logApi.setRemoteIp(ServletUtils.getIpAddr());
        logApi.setRequestUri(ServletUtils.getRequest().getServletPath());
        Map<String, Object> event = new HashMap(16);
        event.put("log", logApi);
        SpringContextHolder.publishEvent(new ApiLogEvent(event));
    }
}
